﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Net.Mail;
using Microsoft.TeamFoundation.Server;
using System.Configuration;

namespace DBSchemaService.Utils
{
    public static class EmailHelper
    {
        internal static readonly List<string> emails = ConfigurationManager.AppSettings["emails"].Split(',').ToList();

        public static void NotifyDeveloper(Identity user, WorkItem wi, int changesetNumber)
        {
            try
            {
                if (user == null)
                    throw new NullReferenceException("Method NotifyDeveloper: Parameter user is null");
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex);
            }

            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(new MailAddress(user.MailAddress, user.DisplayName));

                //set the content
                if (wi.State == "Active")
                {
                    mail.Subject = string.Format("FAIL: The requested schema change {0} ({1}) has failed.", wi.Id, wi.Title);
                    //mail.Body = wi.Fields["Info"].Value.ToString();
                    mail.Body = wi.History.ToString();
                }
                else
                {
                    mail.Subject = string.Format("SUCCESS: The requested schema change {0} ({1}) processed successfully.", wi.Id, wi.Title);                    
                    mail.Body = "<br />";
                    mail.Body = "<br />";
                    mail.Body = string.Format("Changeset Number:{0} ",changesetNumber);
                }
               
                mail.IsBodyHtml = true;

                //send the message
                SmtpClient smtp = new SmtpClient();
                smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                smtp.Send(mail);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public static void NotifyDeveloper(Identity user, WorkItem wi, string sql)
        {
            try
            {
                if (user == null)
                    throw new NullReferenceException("Method NotifyDeveloper: Parameter user is null");
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex);
            }

            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(new MailAddress(user.MailAddress, user.DisplayName));

                //set the content
                if (wi.State == "Active")
                {
                    mail.Subject = string.Format("FAIL: The requested schema change {0} ({1}) has failed.", wi.Id, wi.Title);
                }
                else
                {
                    mail.Subject = string.Format("SUCCESS: The requested schema change {0} ({1}) processed successfully.", wi.Id, wi.Title);
                }

                //mail.Body = wi.Fields["Info"].Value.ToString();
                mail.Body = wi.History.ToString();
                mail.Body += "<br />";
                mail.Body += sql;
                mail.IsBodyHtml = true;

                //send the message
                SmtpClient smtp = new SmtpClient();
                smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                smtp.Send(mail);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public static void NotifySpecifiedAddressSuccessfulSchema(string emailAddress, string displayName, WorkItem wi, string FileThatWasUpdated, int changesetNumber)
        {
            try
            {
                if (emailAddress == null)
                    throw new NullReferenceException("Method NotifySpecifiedAddressSuccessfulSchema: Parameter emailAddress is null");
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex);
            }

            try
            {
                MailMessage mail = new MailMessage();
                
                
                foreach (string theEmail in emails)
                {
                    mail.To.Add(new MailAddress(theEmail));
                }

                //set the content
                mail.Subject = string.Format("{0} Database Change", wi.Fields["Database"].Value);
                mail.Body = string.Format("Please get the latest from TFS and run the following file in your <b>{0} database</b>", wi.Fields["Database"].Value.ToString());
                mail.Body += "<br />";
                mail.Body += "<br />";
                mail.Body += string.Format(FileThatWasUpdated);
                mail.Body += "<br />";
                mail.Body += "<hr />";                
                mail.Body += "<b>Schema Change Information:</b>";
                mail.Body += "<br />";
                mail.Body += wi.Fields["Info"].Value.ToString();
                mail.Body += "<hr />";
                mail.Body += "<b>Schema Change Details:</b>";
                mail.Body += "<br />";
                mail.Body += string.Format("Schema Change Creator: {0}", wi.CreatedBy);
                mail.Body += "<br />";
                mail.Body += string.Format("Schema Change Work Item ID: {0}", wi.Id);
                mail.Body += "<br />";
                mail.Body += string.Format("Schema Change Work Item Title: {0}", wi.Title);
                mail.Body += "<br />";
                mail.Body += string.Format("Schema Change Work Item Area Path: {0}", wi.AreaPath);
                mail.Body += "<br />";
                mail.Body += string.Format("Schema Change Work Item Iteration: {0}", wi.IterationPath);
                mail.Body += "<br />";
                mail.Body += string.Format("Schema Change Changeset #: {0}", changesetNumber); 
                mail.IsBodyHtml = true;

                //send the message
                SmtpClient smtp = new SmtpClient();
                smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public static void NotifySpecifiedAddressSuccessfulSproc(string emailAddress, string displayName, string database, string FileThatWasUpdated, int changesetNumber)
        {
            try
            {
                if (emailAddress == null)
                    throw new NullReferenceException("Method NotifySpecifiedAddressSuccessfulSproc: Parameter emailAddress is null");
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex);
            }

            try
            {
                MailMessage mail = new MailMessage();
               
                foreach (string theEmail in emails)
                {
                    mail.To.Add(new MailAddress(theEmail));
                }

                //set the content
                mail.Subject = string.Format("{0} Database Change", database);
                mail.Body = string.Format("Please get the latest from TFS and run the following file in your <b>{0} database</b>", database);
                mail.Body += "<br />";
                mail.Body += "<br />";
                mail.Body += string.Format(FileThatWasUpdated);
                mail.Body += "<br />";
                mail.Body += "<br />";         
                mail.Body += string.Format("Changeset #: {0}", changesetNumber); 
                mail.IsBodyHtml = true;

                //send the message
                SmtpClient smtp = new SmtpClient();
                smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}
